﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace ZSens
{
    /// <summary>
    /// Interaction logic for ConfigOrientation.xaml
    /// </summary>
    public partial class ConfigOrientation : Window
    {
        private Orientation owner;
        private bool isUp;
        private List<Key> decrease;
        private List<Key> increase;
        public ConfigOrientation(Orientation o)
        {
            InitializeComponent();
            increase = new List<Key>();
            decrease = new List<Key>();
            isUp = true;
            owner = o;
            PreviewKeyUp += ConfigButton_PreviewKeyUp;
            PreviewKeyDown += ConfigButton_PreviewKeyDown;
        }

        void ConfigButton_PreviewKeyUp(object sender, KeyEventArgs e)
        {
            isUp = true;
        }

        private void ConfigButton_PreviewKeyDown(object sender, KeyEventArgs e)
        {
            if (isUp)
            {
                if (increase_tb.IsFocused)
                {
                    increase.Clear();
                    increase_tb.Text = "";
                    isUp = false;

                }
                if (decrease_tb.IsFocused)
                {
                    decrease.Clear();
                    decrease_tb.Text = "";
                    isUp = false;

                }
            }
            if (increase_tb.IsFocused && !increase.Contains(e.Key))
            {
                if (increase.Count == 0)
                    increase_tb.Text = e.Key.ToString();
                else
                    increase_tb.Text += " + " + e.Key.ToString();
                increase.Add(e.Key);
            }
            else if (decrease_tb.IsFocused && !decrease.Contains(e.Key))
            {
                if (decrease.Count == 0)
                    decrease_tb.Text = e.Key.ToString();
                else
                    increase_tb.Text += " + " + e.Key.ToString();
                decrease.Add(e.Key);
            }
        }

        private void Button_Click_1(object sender, RoutedEventArgs e)
        {
            if ((bool)rdbtn_keyboard.IsChecked)
            {
                List<VMultiAPI.Keyboard.KeyCode> increase_keys = new List<VMultiAPI.Keyboard.KeyCode>();
                List<VMultiAPI.Keyboard.KeyCode> decrease_keys = new List<VMultiAPI.Keyboard.KeyCode>();
                foreach (Key key in increase)
                    increase_keys.Add(VMultiAPI.Keyboard.vMultiKeyMap[(int)key]);
                foreach (Key key in decrease)
                    decrease_keys.Add(VMultiAPI.Keyboard.vMultiKeyMap[(int)key]);
                owner.BindedAction = new List<Action>
                {
                    new PressKeyboardKeyAction(
                        new FloatToBool(owner,0.3f,new Range { Start = 0.4f, End = 0f } ),
                         decrease_keys),
                    new PressKeyboardKeyAction (
                        new FloatToBool(owner,0.3f,new Range { Start = 0.6f ,End = 1f} ),
                        increase_keys)
                };
            }
            else
            {
                if (joystick_cb.SelectedIndex == 0)
                {
                    owner.BindedAction = new List<Action>
                    {
                        new SetJoystickAxisAction(owner,JoystickAxis.X)
                    };
                }
                else if (joystick_cb.SelectedIndex == 1)
                {
                    owner.BindedAction = new List<Action>
                    {
                        new SetJoystickAxisAction(owner,JoystickAxis.Y)
                    };
                }
            }
            owner.DisplayString = owner.GetDisplayString();
            Close();

        }

        private void Button_Click_2(object sender, RoutedEventArgs e)
        {
            Close();
        }
    }
}
